blob: 7d5b8cf37d500238ae5bf382ee29dcbfcaef583e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
<script>
import { DateTime } from 'luxon';
import { page } from '$app/stores';
import ActiveChannel from '$lib/components/ActiveChannel.svelte';
import MessageInput from '$lib/components/MessageInput.svelte';
import { channelsList, messages } from '$lib/store';
let channel = $derived($page.params.channel);
let messageRuns = $derived($messages.inChannel(channel));
let activeChannel;
function inView(parentElement, element) {
const parRect = parentElement.getBoundingClientRect();
const parentTop = parRect.top;
const parentBottom = parRect.bottom;
const elRect = element.getBoundingClientRect();
const elementTop = elRect.top;
const elementBottom = elRect.bottom;
return ((parentTop < elementTop) && (parentBottom > elementBottom));
}
function getLastVisibleMessage() {
const parentElement = activeChannel;
const childElements = parentElement.getElementsByClassName('message');
const lastInView = Array.from(childElements).reverse().find((el) => {
return inView(parentElement, el);
});
return lastInView;
}
function setLastRead() {
const channelObject = $channelsList.getChannel(channel);
const lastInView = getLastVisibleMessage();
if (!channelObject || !lastInView) {
return;
}
const at = DateTime.fromISO(lastInView.dataset.at);
// Do it this way, rather than with Math.max tricks, to avoid assignment
// when we don't need it, to minimize reactive changes:
if (at > channelObject.lastReadAt) {
channelObject.lastReadAt = at;
}
}
$effect(() => {
const _ = $messages.inChannel(channel);
setLastRead();
});
function handleKeydown(event) {
if (event.key === 'Escape') {
setLastRead(); // TODO: pass in "last message DT"?
}
}
let lastReadCallback = null;
function handleScroll() {
clearTimeout(lastReadCallback); // Fine if lastReadCallback is null still.
lastReadCallback = setTimeout(setLastRead, 2 * 1000);
}
</script>
<svelte:window onkeydown={handleKeydown} />
<div class="active-channel" on:scroll={handleScroll} bind:this={activeChannel}>
<ActiveChannel {messageRuns} />
</div>
<div class="create-message max-h-full">
<MessageInput {channel} />
</div>
<style>
.active-channel {
height: calc(
100vh - var(--app-bar-height) - var(--interface-padding) - var(--input-row-height)
);
overflow: auto;
}
</style>
|